<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\ShopHair;
use App\Shops;
use App\Hair;
use Illuminate\Validation\Rule;
use DB;
use Input;

class ShopHairController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //获取所有

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //获取店铺id
        $shopid = $request -> id;
        //获取已经具有的hair_style
        $hairids = ShopHair::where([['shopid','=',$shopid],['type','=','2']])
            ->select('value')
            ->get()
            ->toArray();
        foreach ($hairids as $hairid){
            $ids[] = $hairid['value'];
        }
        //选择出没有选中的hair_style
        $datas =  Hair::whereNotIn('hairid',$ids)
            ->select('hairid','hairthumb','hairname')
            ->get();
       if(count($datas) == 'null')
        return back() -> with(['info'=>'您已经添加了全部hair了']);
        //然后获取已经获取的数据
        return view('admin.shophair.add',compact('datas','shopid'));



    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {    //首先进行验证、如果已经添加TYPE为2的信息。则返回
        $this -> validate($request,[
            'value'=>Rule::unique('shops_haircolor')->where(function ($query){$query -> where('type',2);})

        ],[
            'value.unique'=>'有重复添加的数据,请刷新页面,重新添加',
        ]);
         //获取shopid.
        $shopid = Input::get('shopid');
        $type = '2';
        //进行存储
        $hairids = Input::get('value');
        //将数据进行批量插入;
        foreach($hairids as $k=>$hairid){
            $data[$k] = ['type'=>$type,'shopid'=>$shopid,'value'=>$hairid];
        }
        $res  = ShopHair::where('shopid',$shopid)->insert($data);
        if($res)
        return Redirect('shophair_show/'.$shopid)->with(['info'=>'添加成功']);
        return back()->with(['info'=>'添加失败']);


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //获取hair_colortype表中的所有id-colortypeid
        $colors = ShopHair::where('type','1')->select('value')->get()->toArray();
        foreach($colors as $color){
            $colorid[] = $color['value'];
        }
        //获取当前id对应的hair_style.中的所有id-haircolorid
        $haircolorid = ShopHair::where('id',$id)->value('value');
        //查找数据即查找本店铺对应的hair_color里面的数据
        //根据以上数据获取数据
        $datass = DB::table('hair_color')
            ->join('hair_colortype','hair_colortype.colortypeid','=','hair_color.colortypeid')
            ->join('hair_style','hair_style.hairid','=','hair_color.hairid')
            ->where('hair_style.hairid','=',$haircolorid)
            ->whereIn('hair_colortype.colortypeid',$colorid)
            ->get();
        return view('admin.shophair.show',compact('datass'));



    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //根据id获取数据
        return '不支持店铺方面编辑,请在hair管理里面编辑';

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //获取id  直接删除
        $res = ShopHair::destroy($id);
        if($res)
        return Response() -> json(['status'=>'success','msg'=>'删除成功']);
        return Response() -> json(['status'=>'error','msg'=>'删除失败']);

    }
    public function shophair_show($id)
    {

        //然后获取hairstyle
        $datas = DB::table('shops_haircolor')
            ->select('shops_haircolor.id','hair_style.hairname','hair_style.hairthumb')
            ->Join('hair_style','hair_style.hairid','=','shops_haircolor.value')
            ->where([['shops_haircolor.type','=','2'],['shops_haircolor.shopid','=',$id]])
            ->paginate('20');
        return view('admin.shophair.index',compact('datas','id'));
                

    }
}
